package org.capybara.coffeehouse.trigger;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

import org.apache.log4j.Logger;
import org.capybara.coffeehouse.action.Action;
import org.capybara.coffeehouse.event.EventListener;

/**
 * Class that represents a set of conditions and an action to take if they come true.
 * @author dfraser
 *
 */
public class Trigger implements EventListener {	
	private List<String> conditions = new ArrayList<>();
	private Action action;
	private final Logger log = Logger.getLogger(Trigger.class.getName());
	
	public Trigger(List<String> conditions, Action action) {
		this.action = action;
		this.conditions = conditions;
	}
	
	@Override
	public void handleEvent(EventObject e) {
		log.info("handling event!");
		// check to see if the conditions are met
		boolean conditionsMet = false;
		for (String cond : conditions) {
			log.info("event class: "+e.getClass().getName());
			if (e.getClass().getName().equals(cond)) {
				conditionsMet = true;
			}
		}
		// do the action
		if (conditionsMet) {
			log.info("executing action...");
			action.execute();
		}
	}
}
